package com.xz.knowledgeDuplication.exception;

import com.xz.knowledgeDuplication.result.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 *
 * @author 召
 * @date 2021-12-26 21:26:57
 */
@ControllerAdvice
public class ExceptionAop {

    /**
     * 如果controller 钟抛出的是AppException 异常 就跑到这个方法钟来处理
     *
     * @param ex 抛出的异常对象
     * @return
     */
    @ExceptionHandler(AppException.class)
    @ResponseBody
    public R doAppException(AppException ex) {
        return new R(ex.getCode(), ex.getMsg(), null);
    }

    /**
     * 捕获落网之鱼
     *
     * @param ex 抛出的异常对象
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R doException(Exception ex) {
        ex.printStackTrace();
        return new R("500", ex.getMessage(), null);
    }
}
